﻿@{
    ViewData["Title"] = "编辑物料产品";
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->
    <link href="//unpkg.com/layui@2.9.17/dist/css/layui.css" rel="stylesheet">
</head>
<body>
    <!-- 给容器追加 class="layui-form-pane"，即可显示为方框风格 -->
    <form class="layui-form layui-form-pane" action="">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">物料编码</label>
                <div class="layui-input-block">
                    <input type="text" name="ItemCode" id="ItemCode" autocomplete="off" class="layui-input" lay-verify="required">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">物料名称</label>
                <input type="checkbox" checked lay-skin="switch" lay-filter="switchTest" title="自动生成">
                <div class="layui-input-inline">
                    <input type="text" name="ItemName" id="ItemName" autocomplete="off" class="layui-input" lay-verify="required">
                </div>
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">规格型号</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入规格型号" class="layui-textarea" name="Specification" id="Specification" lay-verify="required"></textarea>
            </div>
        </div>
        <div class="layui-form-item">

            <div class="layui-form-item">
                <label class="layui-form-label">单位</label>
                <div class="layui-input-inline">
                    <select name="UnitOfMeasure" lay-filter="aihao" lay-verify="required">
                        <option value=""></option>
                        <option value="个">个</option>
                        <option value="袋">袋</option>
                        <option value="厘米">厘米</option>
                        <option value="分米">分米</option>
                        <option value="毫米">毫米</option>
                    </select>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-form-item">
                <label class="layui-form-label">物料/产品分类</label>
                <div class="layui-input-inline">
                    <select name="ItemOrProduct" lay-filter="aihao" lay-verify="required">
                        <option value=""></option>
                        <option value="item">物料</option>
                        <option value="product">产品</option>
                    </select>
                </div>
            </div>
        </div>

        <div class="layui-form-item" pane>
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-block">
                <input type="radio" name="EnableFlag" value="Y" title="是" checked id="EnableFlagY">
                <input type="radio" name="EnableFlag" value="N" title="否" id="EnableFlagN">
            </div>
        </div>

        <div class="layui-form-item" pane>
            <label class="layui-form-label">安全库存</label>
            <div class="layui-input-block">
                <input type="radio" name="SafeStockFlag" value="Y" title="是" checked id="SafeStockFlagY">
                <input type="radio" name="SafeStockFlag" value="N" title="否" id="SafeStockFlagN">
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容" class="layui-textarea" name="Remark" id="Remark" lay-verify="required"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-form-item">
                <button class="layui-btn" lay-submit lay-filter="demo-update">修改</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
    <script src="//unpkg.com/layui@2.9.17/dist/layui.js"></script>
    <script>
        layui.use(['form'], function () {
            var layer = layui.layer;
            var form = layui.form;
            
            function getParentVal(){
                $("#ItemCode").val(parent.ItemCode);
                $("#ItemName").val(parent.ItemName);
                $("#Remark").val(parent.Remark);
                $("#Specification").val(parent.Specification);

                if (parent.EnableFlag == "Y") {
                    $("#EnableFlagY").prop("checked", true);
                } else {
                    $("#EnableFlagN").prop("checked", true);
                }

                if (parent.SafeStockFlag == "Y") {
                    $("#SafeStockFlagY").prop("checked", true);
                } else {
                    $("#SafeStockFlagN").prop("checked", true);
                }

                form.render("select");//重新渲染表单
                form.render();//重新渲染表单
            }
            getParentVal();
            // 表单提交事件
            form.on('submit(demo-update)', function (data) {
                var field = data.field; // 获取表单字段值
                console.log(field.ItemCode);

                var UpdateDto = {
                    //获取表单
                    ItemCode: field.ItemCode,
                    ItemName: field.ItemName,
                    Specification: field.Specification,
                    UnitOfMeasure: field.UnitOfMeasure,
                    ItemOrProduct: field.ItemOrProduct,
                    EnableFlag: field.EnableFlag,
                    SafeStockFlag: field.SafeStockFlag,
                    Remark: field.Remark,

                    //数据库值
                    Id: parent.Id,
                    ItemTypeId: parent.ItemTypeId,
                    ItemTypeCode: parent.ItemTypeCode,
                    ItemTypeName: parent.ItemTypeName,
                    MinStock: parent.MinStock,
                    MaxStock: parent.MaxStock,
                    CreateBy: parent.CreateBy,
                    CreateTime: parent.CreateTime
                }
                $.ajax({
                    url: '/Meng/UpdateMdItem',
                    type: 'Get',
                    data: UpdateDto,
                    dataType: "json",
                    success: function (res) {
                        console.log(res);
                        if (res ==1) {
                            
                            layer.msg('保存成功', {
                                icon: 1,
                                time: 1000
                            }, function () {
                                var index = parent.layer.getFrameIndex(window.name);//先得到当前iframe层的索引
                                parent.layer.close(index);//再执行关闭
                            });
                        }
                        else {
                            alert("修改失败");
                        }

                    }
                });
                return false;
            });
        });

    </script>

</body>
</html>